﻿CREATE PROCEDURE [dbo].[proc_Finance_RunningAccountCopy_Search]
	(
		@CompanyId int,
		@SText nvarchar(50),
		@StartIndex int,
		@EndIndex int
	)
AS
	begin
	declare @strWhere NVARCHAR(4000)
	set @strWhere=''
	if @SText <>''
	begin
	set @strWhere=' and ( R.Summary like ''%'+@SText+'%'' Or R.Income like ''%'+@Stext+'%'')'
	end
	set @strWhere='
		;WITH list As(Select top 200 ROW_NUMBER() OVER (ORDER BY  R.TradeDate DESC)AS Row,
			R.Id,
			R.CreateDate,
			R.TradeDate,
			R.Summary,
			R.AccountsTypeName,
			R.PaymentTypeName,
			R.BankName,
			R.Income,
			R.Outlay,
			R.EName,
			BillNumber,
			Description
		From Finance_RunningAccountCopy R
		Where R.CompanyId='+Convert(varchar,@CompanyId)+' and R.AccountsTypeId=1 
		'+@strWhere+'
		)
		Select *,(SELECT count(1) FROM list) AS cid 
		From list  Where Row Between '+Convert(varchar,@StartIndex)+' and '+Convert(varchar,@EndIndex)+'
		'
		exec (@strWhere)
	end
	RETURN
